<template>
  <el-dialog title="分配角色" :visible="showRoleDialog" @click="btnCancel">
    <!-- 多选框组 -->
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id" >
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>
    <!-- 定义footer的插槽 -->
    <el-row slot="footer" type="flex" justift="center">
      <el-col :span="6">
        <el-button type="primary" size="small" @click="btnOk">确定</el-button>
        <el-button size="small" @click="btnCancel">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getRoleList } from '@/api/setting'
import { getUserDetailById } from '@/api/user'
import { assignRoles } from '@/api/employess'
export default {
  data () {
    return {
      list: [], // 存储当前所有角色id
      roleIds: []  // 这个数组负责存储 当前用户拥有的角色id
    }
  },
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false
    },
    userId: {
      // 处理当前用户
      type: String,
      default: null,
    }
  },
  created() {
    // 获取所有的角色
    this.getRoleList()
  },
  methods: {
   async getRoleList() {
     const { rows } = await getRoleList({ page:1, pagesize: 20})  // 不传值 默认就是十条
    this.list = rows
    },
    // 
    async getUserDetailById(id) {
    const { roleIds } = await getUserDetailById(id)
     this.roleIds = roleIds
    },
    async btnOk() {
      await assignRoles({ id: this.userId, roleIds: this.roleIds })
      // 
      this.$emit('update: showRoleDialog', false)
    },
    btnCancel() {
      // 清空数组  
      this.roleIds = []
      this.$emit('update: showRoleDialog', false)
    },
  }
}
</script>

<style>

</style>